package com.clinic.action.drugstock;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.action.common.AuthenticationAction;
import com.clinic.entity.gui.GuiDrug;
import com.clinic.entity.gui.GuiDrugStock;
import com.clinic.entity.gui.GuiPagination;
import com.clinic.entity.param.Index;
import com.clinic.entity.type.SortProperty;
import com.clinic.logic.drug.DrugLogic;
import com.clinic.logic.drug.DrugStockLogic;
import com.clinic.util.CommonUtil;

public class DrugStockAction extends AuthenticationAction {

	private static final long serialVersionUID = 397135899553041558L;
	
	private long id;
	private long drugId;
	private String term;
	private SortProperty sort;
	private int size; 
	private int page;
	private GuiDrug guiDrug;
	private GuiDrugStock guiDrugStock;
	private GuiPagination guiPagination;
	private List<GuiDrug> guiDrugList;
	private List<GuiDrugStock> guiDrugStockList;
			
	@Autowired
	private DrugLogic drugLogic;
	@Autowired
	private DrugStockLogic drugStockLogic;
	
	public String registration() {
		if(isDoctorLogin() || isAssistantLogin()) {
			guiDrugList = drugLogic.getDrugList(new Index(1000));
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
		
	}

	public String show(){
		if (isDoctorLogin() || isAssistantLogin()) {
			guiDrugStock = drugStockLogic.getDrugStockById(id);
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String edit(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiDrugList = drugLogic.getDrugList(new Index(1000));
			guiDrugStock = drugStockLogic.getDrugStockById(id);
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String update(){
		if(isDoctorLogin() || isAssistantLogin()) {			
			/*boolean isValidDrugStockUpdate = drugStockLogic.isValidDrugStockUpdate(guiDrugStock);
			if(!isValidDrugStockUpdate) {
				addActionError("Failed to update.This drug stock is available");
			}*/
			
			boolean isDrugEmpty = drugStockLogic.isDrugEmpty(guiDrugStock);
			if(isDrugEmpty) {
				addActionError("Failed to update.Please select a drug");
			}
			
			boolean isOrderAmountEmpty = drugStockLogic.isOrderAmountEmpty(guiDrugStock);
			if(isOrderAmountEmpty) {
				addActionError("Failed to update.Please enter drug order amount");
			}
			
			boolean isValidExpireDate = drugStockLogic.isValidExpireDate(guiDrugStock);
			if(!isValidExpireDate) {
				addActionError("Failed to update.Not a valid expire date");
			}
			
			if(hasActionErrors()) {
				guiDrugList = drugLogic.getDrugList(new Index(1000));
				return ActionResult.ERROR;
			} else {
				guiDrugStock = drugStockLogic.updateDrugStock(guiDrugStock);				
			}	
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String insert(){
		if(isDoctorLogin() || isAssistantLogin()) {			
			/*boolean isValidDrugStock = drugStockLogic.isValidDrugStockInsert(guiDrugStock);
			if(!isValidDrugStock) {
				addActionError("Failed to insert.This drug stock is available");
			}*/
			
			boolean isOrderAmountEmpty = drugStockLogic.isOrderAmountEmpty(guiDrugStock);
			if(isOrderAmountEmpty) {
				addActionError("Failed to insert.Please enter drug order amount");
			}
			
			boolean isStockAmountEmpty = drugStockLogic.isStockAmountEmpty(guiDrugStock);
			if(isStockAmountEmpty) {
				addActionError("Failed to insert.Please enter drug stock amount");
			}
			
//			boolean isStockAmountValid = drugStockLogic.isStockAmountValid(guiDrugStock);
//			if(!isStockAmountValid) {
//				addActionError("Failed to insert.Initial stock amount should be equal to the order amount.");
//			}
			
			boolean isExpireDateEmpty = drugStockLogic.isExpireDateEmpty(guiDrugStock);
			if(isExpireDateEmpty) {
				addActionError("Failed to insert.Please select expire date");
			}
			
			boolean isValidExpireDate = drugStockLogic.isValidExpireDate(guiDrugStock);
			if(!isValidExpireDate) {
				addActionError("Failed to insert.Not a valid expire date");
			}
			
			if(hasActionErrors()) {
				guiDrugList = drugLogic.getDrugList(new Index(1000));
				return ActionResult.ERROR;
			} else {
				guiDrugStock = drugStockLogic.insertDrugStock(guiDrugStock);
			}
			return ActionResult.SUCCESS;					
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String delete(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiDrugStock = drugStockLogic.getDrugStockById(id);
			if(guiDrugStock.isAvailable()) {
				drugStockLogic.deleteDrugStockDetailsById(id);
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String list(){
		if(isDoctorLogin() || isAssistantLogin()) {		
			Index index = CommonUtil.getIndexBypage(page);
			if (StringUtils.isNotBlank(term) || sort != null) {
				guiDrugStockList = drugStockLogic.getDrugStocksByTerm(term, sort.getColumn(), index);
				guiPagination = drugStockLogic.getPaginationOfDrugStocksByTerm(term, page);
				size = guiPagination.getTotalCount();
			} else if (drugId > 0) {
				guiDrug = drugLogic.getDrugDetailsById(drugId);
				if (guiDrug.isAvailable()) {
					guiDrugStockList = drugStockLogic.getDrugStocksByDrugId(drugId, index);
					guiPagination = drugStockLogic.getPaginationOfDrugStocksByDrugId(drugId, page);
					size = guiPagination.getTotalCount();
				}
			} else {
				guiDrugStockList = drugStockLogic.getDrugStocks(index);
				guiPagination = drugStockLogic.getPaginationOfDrugStocks(page);
				size = guiPagination.getTotalCount();
			}			
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getDrugId() {
		return drugId;
	}

	public void setDrugId(long drugId) {
		this.drugId = drugId;
	}

	public boolean isGuiDrugExist() {
		return guiDrug != null;
	}
	
	public GuiDrug getGuiDrug() {
		return guiDrug;
	}

	public void setGuiDrug(GuiDrug guiDrug) {
		this.guiDrug = guiDrug;
	}

	public GuiDrugStock getGuiDrugStock() {
		return guiDrugStock;
	}

	public void setGuiDrugStock(GuiDrugStock guiDrugStock) {
		this.guiDrugStock = guiDrugStock;
	}

	public List<GuiDrug> getGuiDrugList() {
		return guiDrugList;
	}

	public void setGuiDrugList(List<GuiDrug> guiDrugList) {
		this.guiDrugList = guiDrugList;
	}

	public List<GuiDrugStock> getGuiDrugStockList() {
		return guiDrugStockList;
	}

	public void setGuiDrugStockList(List<GuiDrugStock> guiDrugStockList) {
		this.guiDrugStockList = guiDrugStockList;
	}
	
	public boolean isTermExist() {
		return StringUtils.isNotBlank(term);
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}
	
	public boolean isSortExist() {
		return (sort != null);
	}

	public SortProperty getSort() {
		return sort;
	}

	public void setSort(String sortProperty) {
		this.sort = SortProperty.getValue(sortProperty);
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public GuiPagination getGuiPagination() {
		return guiPagination;
	}

	public void setGuiPagination(GuiPagination guiPagination) {
		this.guiPagination = guiPagination;
	}
	
}
